# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@io_bazel_rules_docker//container:container.bzl", "container_push")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_k8s//k8s:object.bzl", "k8s_object")
load("//bazel:go_image_alias.bzl", "go_image")

go_library(
    name = "elastic_migration_lib",
    srcs = ["job.go"],
    importpath = "px.dev/pixie/src/cloud/jobs/elastic_migration",
    visibility = ["//visibility:private"],
    deps = [
        "//src/cloud/jobs/elastic_migration/controllers",
        "//src/cloud/jobs/elastic_migration/schema",
        "//src/cloud/shared/esutils",
        "@com_github_sirupsen_logrus//:logrus",
        "@com_github_spf13_pflag//:pflag",
        "@com_github_spf13_viper//:viper",
    ],
)

go_binary(
    name = "elastic_migration",
    embed = [":elastic_migration_lib"],
    visibility = ["//src/cloud:__subpackages__"],
)

go_image(
    name = "elastic_migration_image",
    binary = ":elastic_migration",
    importpath = "px.dev/pixie",
)

container_push(
    name = "push_elastic_migration_image",
    format = "Docker",
    image = ":elastic_migration_image",
    registry = "gcr.io",
    repository = "pixie-oss/pixie-dev/cloud/elastic_migration_image",
    tag = "{STABLE_BUILD_TAG}",
)

## Usage for the following objects
## Build for dev.
# $ bazel run :create_es_index_dev --define es_index=new_index_name
## Build for staging.
# $ bazel run :create_es_index_prod --define es_index=new_index_name
## Build for prod.
# $ bazel run :create_es_index_staging --define es_index=new_index_name
## If the index already exists for any of these, then fail.

k8s_object(
    name = "create_es_index_dev",
    images = {"gcr.io/pixie-oss/pixie-dev/cloud/elastic_migration_image:latest": ":elastic_migration_image"},
    kind = "job",
    substitutions = {
        "{es_index}": "$(es_index)",
        "{mapping_file}": "$(mapping_file)",
        "{namespace}": "plc-dev",
    },
    tags = ["manual"],
    template = ":elastic_migration_job.yaml",
)

k8s_object(
    name = "create_es_index_staging",
    images = {"gcr.io/pixie-oss/pixie-dev/cloud/elastic_migration_image:latest": ":elastic_migration_image"},
    kind = "job",
    substitutions = {
        "{es_index}": "$(es_index)",
        "{mapping_file}": "$(mapping_file)",
        "{namespace}": "plc-staging",
    },
    tags = ["manual"],
    template = ":elastic_migration_job.yaml",
)

k8s_object(
    name = "create_es_index_prod",
    images = {"gcr.io/pixie-oss/pixie-prod/cloud/elastic_migration_image:latest": ":elastic_migration_image"},
    kind = "job",
    substitutions = {
        "{es_index}": "$(es_index)",
        "{mapping_file}": "$(mapping_file)",
        "{namespace}": "plc",
    },
    tags = ["manual"],
    template = ":elastic_migration_job.yaml",
)
